package easytalk.dialogs;
import java.util.Date;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

import common.Discussion;
import common.DiscussionSentence;
import common.DiscussionState;
import common.Locutor;
import common.DiscussionSentence.MessageType;
import communication.HtmlMessageFormat;


public class ChatDialog extends TitleAreaDialog  {


	private Browser chatText;
	private Text outputText;
	private Discussion discussion;
	private HtmlMessageFormat format; 
	

	public ChatDialog(Shell parentShell, Discussion discussion) {
		super(parentShell);
		setShellStyle(SWT.DIALOG_TRIM | SWT.MODELESS | SWT.RESIZE);
		setBlockOnOpen(false);
		this.discussion = discussion;
		format = new HtmlMessageFormat(discussion);
		initComponents();
	}

	private void initComponents() {

	}

	@Override
	protected Control createContents(Composite parent) {

		Control content = super.createContents(parent);
		Locutor locutor =  discussion.getLocutors().get(0);
		setTitleImage(locutor.getBigImage());
		setTitle(Messages.getString("ChatDialog.0") + locutor.getName()); //$NON-NLS-1$
		return content;
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginWidth = 5;
		gridLayout.marginHeight = 5;
		gridLayout.verticalSpacing = 5;
		gridLayout.horizontalSpacing = 5;
		gridLayout.makeColumnsEqualWidth = true;
		parent.setLayout(gridLayout);

		chatText = new Browser(parent, SWT.BORDER);
		chatText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		chatText
				.setText(Messages.getString("ChatDialog.1")); //$NON-NLS-1$

		outputText = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
		outputText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
				1, 1));
		outputText.setText(Messages.getString("ChatDialog.2")); //$NON-NLS-1$

		outputText.addKeyListener(new KeyListener() {

			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyReleased(KeyEvent e) {
				if ((e.keyCode == 13) || (e.keyCode == 16777296)) {
					String toBeSent = outputText.getText();
					DiscussionSentence sentence = new DiscussionSentence(toBeSent, new Date(), discussion,
							discussion.getMe(), MessageType.NORMAL);
					discussion.getSentences().add(sentence);
					format.addSentence(sentence);
					chatText.setText((String) format.getMessageFormat(discussion));
					
					for (Locutor locutor : discussion.getLocutors()) {
						locutor.getParentSet().getConnection().sendMessage(locutor.getId(), sentence);
					}
					outputText.setText(Messages.getString("ChatDialog.3")); //$NON-NLS-1$
				}
			}

		});

		return parent;
	}

	@Override
	protected Point getInitialSize() {
		// TODO Auto-generated method stub
		return super.getInitialSize();
	}

	@Override
	protected Label getTitleImageLabel() {
		// TODO Auto-generated method stub
		return super.getTitleImageLabel();
	}

	@Override
	protected Control createButtonBar(Composite parent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Control createHelpControl(Composite parent) {
		// TODO Auto-generated method stub
		return super.createHelpControl(parent);
	}

	@Override
	protected Layout getLayout() {
		// TODO Auto-generated method stub
		return super.getLayout();
	}




	public void openInANewthread() {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			@Override
			public void run() {
				open();
			}

		});

	}

	@Override
	public boolean close() {
		discussion.setState(DiscussionState.CLOSED);
		ChatDialogManager.getInstance().removeDiscussion(discussion);
		return super.close();
	}

	public void setDiscussion(Discussion discussion) {
		this.discussion = discussion;
	}

	public Discussion getDiscussion() {
		return discussion;
	}

	public void updateDiscussion(Discussion discussion2) {
		
		this.discussion = discussion2;	
		format.setDiscussion(discussion);
		
		getShell().getDisplay().asyncExec(new Runnable() {

			@Override
			public void run() {
				chatText.setText((String) format.getMessageFormat(discussion));			
		}
		});
	}

}